<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>17</title>
  <style>
    .container {
      width: 400px;
      height: 500px;
      background-color: chocolate;
    }

    .child {
      width: 100px;
      height: 100px;
      background-color: darkorchid;
    }
  </style>
</head>

<body>
  <div class="container" id="container" onclick="test1()">
    <div class="child" id="child" onclick="test2()">Click</div>
  </div>

  <a href="http://www.mi.com" id="mi-btn">小米官网</a>


  <script>
    document.getElementById('container').onclick = function () {
      console.log("container");
    }

    document.getElementById('child').onclick = function () {
      console.log("child");
      // 兼容性写法
      if (event.stopPropagation) {
        // 阻止事件向外冒泡
        event.stopPropagation();
      } else if (event.cancelBubble) {
        event.cancelBubble = true;
      }
    }


    document.getElementById('mi-btn').onclick = function (e) {
      console.log("阻止 a 链接跳转！");
      // 阻止链接跳转有两种方式：
      e.preventDefault();
      // return false;
    }

  </script>

</body>

</html>